﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DataLibrary;
using Common;

namespace TicketSystem
{
    public partial class UC_Travel : UserControl
    {
        public UC_Travel()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();
        private PublicMethod pm = new PublicMethod();

        private void UC_Travel_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        /// <summary>
        /// 旅行社列表
        /// </summary>
        public  void LoadData()
        {
            var department = tse.Departments.Where(u => true).Select(u => new { u.DepartmentName, u.DepartmentTypes.TypeName, u.ContactNumber, u.Fax, u.Address, u.Bank, u.AccountNumber, u.TaxNumber }).ToList();
            this.dataGridView1.DataSource = department;
            this.dataGridView1.Columns[0].HeaderText = "业务单位名称";
            this.dataGridView1.Columns[1].HeaderText = "所属业务部门";
            this.dataGridView1.Columns[2].HeaderText = "联系电话";
            this.dataGridView1.Columns[3].HeaderText = "传真";
            this.dataGridView1.Columns[4].HeaderText = "地址";
            this.dataGridView1.Columns[5].HeaderText = "开户行";
            this.dataGridView1.Columns[6].HeaderText = "银行账号";
            this.dataGridView1.Columns[7].HeaderText = "税号";
            //int roleid = tse.Operators.Single(u => u.OperatorID == 1).RoleID;
            int roleid = tse.Operators.Where(u => u.OperatorID == GetOperator.OperatorID).FirstOrDefault().RoleID;
            List<string> l = new List<string>();
            var right = tse.RoleRights.Where(u => u.RoieID == roleid);
            foreach (var r in right)
            {
                l.Add(r.Rights.RightName);
            }
            pm.CanExport(this.btnExport, l);
            pm.CanPrint(this.btnPrint, l);
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (Form_TravelAdd ta = new Form_TravelAdd())
            {
                ta.ShowDialog();
                //这里调用绑定函数
                LoadData();
            }
        }

        private void btnUpdata_Click(object sender, EventArgs e)
        {
            int rows = this.dataGridView1.SelectedRows.Count;
            if (rows > 1)
            {
                MessageBox.Show("一次只能修改一行");
            }
            else if (rows < 1)
            {
                MessageBox.Show("你当前没有选中任何行");
            }
            else
            {
                string departmentname = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();

                using (Form_TravelUpdate tu = new Form_TravelUpdate())
                {
                    tu.DepartmentName = departmentname;
                    tu.ShowDialog();
                    //这里调用绑定函数
                    LoadData();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var count = this.dataGridView1.SelectedRows.Count;
            if (count > 0)
            {
                if (MessageBox.Show("确认删除？", "此删除将一并删除关联的旅行社和导游", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    for (int i = 0; i < count; i++)
                    {
                        var deparntmentname = this.dataGridView1.SelectedRows[i].Cells[0].Value.ToString();
                        var department = tse.Departments.Where(u => u.DepartmentName == deparntmentname);
                        tse.Departments.RemoveRange(department);
                        var dg = tse.DepartmentsGuidesSet.Where(u => u.DepartmentID == department.FirstOrDefault().DepartmentID);
                        foreach (var _guide in dg)
                        {
                            if (tse.Guides.Any(u => u.GuideID == _guide.GuideID))
                            {
                                var guide = tse.Guides.Find(_guide.GuideID);
                                tse.Guides.Remove(guide);
                            }
                        }
                        tse.DepartmentsGuidesSet.RemoveRange(dg);
                        //var guide = tse.Guides.Where(u => u.DepartmentsGuides1.LastOrDefault().Departments.DepartmentName == deparntmentname);
                        //tse.Guides.RemoveRange(guide);

                    }
                    int result = tse.SaveChanges();
                    if (result >= 1)
                    {

                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show("删除失败了！");
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                MessageBox.Show("你当前没有选中任何行");
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            var printPreviewDialog = new PrintPreviewDialog { Document = printDocument1 };
            printPreviewDialog.ShowDialog();
        }

        private GridPrinter InitializePrinting()
        {
            var printDialog = new PrintDialog();
            if (printDialog.ShowDialog() != DialogResult.OK)
                return new GridPrinter();
            printDocument1.DocumentName = "旅行社基本信息";
            printDocument1.PrinterSettings = printDialog.PrinterSettings;
            printDocument1.DefaultPageSettings = printDialog.PrinterSettings.DefaultPageSettings;
            printDocument1.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(40, 40, 40, 40);
            var gridPrinter = new GridPrinter(dataGridView1, printDocument1, true, true, "旅行社基本信息",
                new System.Drawing.Font("黑体", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Blue, true);
            return gridPrinter;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {            
            pm.export(this.dataGridView1);
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //var gp = InitializePrinting();
            //var datagrid = dataGridView1;
            //try
            //{
            //    var more = gp.DrawDataGridView(e.Graphics, datagrid);
            //    if (more)
            //        e.HasMorePages = true;
            //}
            //catch
            //{
            //    MessageBox.Show("打印异常");
            //}
            Common.PrintDataGridView.Print(this.dataGridView1, "旅行社信息", "", 0, 0);
        }

    }
}
